home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / optivc16 / cemath.h < prev    next >
C/C++ Source or Header  |  1999-03-06  |  17KB  |  509 lines

  1. /*    CEMATH.H
  2.  
  3.       Complex library for the languages C and C++.
  4.  
  5.       This header file contains all definitions for
  6.       extended-precision complex numbers (complex long double).
  7.  
  8.       Copyright (C) 1996-1999 Martin Sander
  9.       Address of the author:
  10.            Dr. Martin Sander Software Dev.
  11.            Sertuernerstr. 11
  12.            D-37085 Goettingen
  13.            Germany
  14.            MartinSander@Bigfoot.com
  15.            http://www.optivec.com
  16.  
  17. */
  18.  
  19.  
  20. #ifndef __CEMATH_H
  21. #define __CEMATH_H
  22.  
  23. #if !defined( _CMATH_DEFS )
  24.    #ifdef __BORLANDC__
  25.        #pragma option -a-
  26.    #else /* Visual C++, Optima++ */
  27.        #pragma pack( push,1 )
  28.    #endif /* avoid insertion of dummy bytes  */
  29.    typedef struct {float    Re, Im;} fComplex;
  30.    typedef struct {double   Re, Im;} dComplex;
  31.    #ifdef __BORLANDC__
  32.        typedef long double    extended;
  33.        typedef struct {extended Re, Im;} eComplex;
  34.        #pragma option -a.
  35.    #else /* Visual C++, Optima++ */
  36.        typedef  double extended; /* No support of 80-bit IEEE numbers.
  37.                                  So make extended equal to double    */
  38.        typedef dComplex  eComplex;
  39.        #pragma pack( pop )
  40.    #endif    /* restore default data packing  */
  41.    typedef fComplex fcomplex;
  42.    typedef dComplex dcomplex;
  43.    typedef eComplex ecomplex;  // tolerate lower case
  44.    #define _CMATH_DEFS
  45. #endif
  46. #ifdef __BORLANDC__
  47.     #include <_defs.h>
  48.     #if defined __TINY || defined __SMALL__ || defined __MEDIUM__
  49.         #if defined(_RTLDLL) || defined(_CLASSDLL)
  50.             #error Must use static BC Runtime Library with OptiVec and CMATH in models TINY, SMALL, MEDIUM
  51.         #endif
  52.         #define   _VFAR  near   /* even in case of DS!=SS  */
  53.     #elif defined __FLAT__
  54.         #define  _VFAR
  55.     #else
  56.         #define   _VFAR  far
  57.     #endif
  58.     #if (__BORLANDC__ >= 0x450)
  59.          #define __cmf _RTLENTRY _EXPFUNC
  60.     #else
  61.          #define __cmf  _Cdecl _FARFUNC
  62.     #endif
  63.     #if __BORLANDC__ < 0x500
  64.         #define VBOOL int
  65.     #else
  66.         #define VBOOL bool
  67.     #endif
  68. #else  /* Visual C++, Optima++ */
  69.     #define _VFAR
  70.     #define __cmf  __cdecl
  71.     #ifndef __CDMATH_H
  72.         #include <cdmath.h>
  73.     #endif
  74. #endif
  75. #define _VFARC const _VFAR
  76.  
  77. #ifdef __BORLANDC__   /* 80-bit IEEE numbers supported:
  78.                          the following 340 lines apply
  79.                          only to Borland C++  */
  80. #ifdef __cplusplus
  81.     /*  first the constructors:
  82.       since eComplex is declared as a struct instead of a class,
  83.       the constructor cannot get the name "eComplex" here.     */
  84.   #ifndef _ECPLX_DEFINED
  85.   inline eComplex __cmf ecplx( extended __ReVal )
  86.   {   eComplex Result;
  87.       Result.Re = __ReVal;
  88.       Result.Im = 0.0;
  89.       return Result;
  90.   }
  91.  
  92.       // up-conversions from single and double precision:
  93.   inline eComplex __cmf ecplx( fComplex _VFARC & __zf )
  94.   {   eComplex Result;
  95.       Result.Re = __zf.Re;
  96.       Result.Im = __zf.Im;
  97.       return Result;
  98.   }
  99.  
  100.   inline eComplex __cmf ecplx( dComplex _VFARC & __zd )
  101.   {   eComplex Result;
  102.       Result.Re = __zd.Re;
  103.       Result.Im = __zd.Im;
  104.       return Result;
  105.   }
  106.  
  107.   #ifdef __COMPLEX_H
  108.         // conversion from class complex
  109.      inline eComplex __cmf ecplx( complex _VFARC & __zc )
  110.      {   eComplex Result;
  111.          Result.Re = real(__zc);
  112.          Result.Im = imag(__zc);
  113.          return Result;
  114.      }
  115.   #endif  // __COMPLEX_H
  116.   #define _ECPLX_DEFINED
  117.   #endif  // _ECPLX_DEFINED
  118. #endif   /* __cplusplus */
  119.        /* basic form of constructor for C and C++ : */
  120. eComplex __cmf ecplx( extended __ReVal, extended __ImVal);
  121.  
  122.      /* conversion from fComplex and dComplex */
  123. #if !defined __NEWCPLX_H
  124.     #if defined __cplusplus
  125.         extern "C" {
  126.     #endif
  127.     eComplex __cmf cftoce( fComplex __zf );
  128.     eComplex __cmf cdtoce( dComplex __zd );
  129.     #if defined __cplusplus
  130.         }
  131.     #endif
  132. #endif
  133.  
  134.    /* Basic complex operations. They are defined both
  135.    for C and C++. However, for C++ you may as well use the
  136.    overloaded operators and functions defined further below. */
  137. #define         ce_real( z )  (z).Re
  138. #define         ce_imag( z )  (z).Im
  139. #if defined __cplusplus && !defined _CMATH_CLASSDEFS 
  140. extern "C" {  // the following functions cannot be "extern C",
  141. #endif       // if eComplex identical with complex<extended>
  142. eComplex __cmf  ce_neg(  eComplex __z );
  143. eComplex __cmf  ce_conj( eComplex __z );
  144. #ifdef __cplusplus  // even if _CMATH_CLASSDEFS
  145.     extern "C" extended __cmf  ce_norm( eComplex __z );
  146.     extern "C" extended __cmf  ce_arg(  eComplex __z );
  147. #else
  148.     extended __cmf  ce_norm( eComplex __z );
  149.     extended __cmf  ce_arg(  eComplex __z );
  150. #endif
  151. eComplex __cmf  ce_polar( extended __mag, extended __angle );
  152.  
  153. eComplex __cmf  ce_add(   eComplex __x, eComplex __y );
  154. eComplex __cmf  ce_addRe( eComplex __x, extended __yRe );
  155. eComplex __cmf  ce_sub(   eComplex __x, eComplex __y );
  156. eComplex __cmf  ce_subRe( eComplex __x, extended __yRe );  /* x - yRe */
  157. eComplex __cmf  ce_subrRe( eComplex __x, extended __yRe ); /* yRe - x */
  158. eComplex __cmf  ce_mul(   eComplex __x, eComplex __y );
  159. eComplex __cmf  ce_mulRe( eComplex __x, extended __yRe );
  160. eComplex __cmf  ce_div(   eComplex __x, eComplex __y );
  161. eComplex __cmf  ce_divRe( eComplex __x, extended __yRe );  /*  x / yRe  */
  162. eComplex __cmf  ce_divrRe( eComplex __x, extended __yRe ); /* yRe / x   */
  163.  
  164. /*  mathematical functions with error handling through _matherr: */
  165. #ifdef __cplusplus  // even if _CMATH_CLASSDEFS
  166.     extern "C" extended __cmf  ce_abs(  eComplex __z );
  167. #else
  168.     extended __cmf  ce_abs(  eComplex __z );
  169. #endif
  170. eComplex __cmf  ce_acos( eComplex __z );
  171. eComplex __cmf  ce_asin( eComplex __z );
  172. eComplex __cmf  ce_atan( eComplex __z );
  173. eComplex __cmf  ce_cos(  eComplex __z );
  174. eComplex __cmf  ce_cosh( eComplex __z );
  175. eComplex __cmf  ce_cubic( eComplex __z );  /* raise to the third power */
  176. eComplex __cmf  ce_exp(  eComplex __z );
  177. eComplex __cmf  ce_inv(  eComplex __z );    /*   1.0 / z   */
  178. eComplex __cmf  ce_ipow( eComplex __z, int __exponent );
  179.                                              /* raise z to integer power */
  180. eComplex __cmf  ce_ln(    eComplex __z );
  181. eComplex __cmf  ce_log(   eComplex __z ); /* same as ce_ln */
  182. eComplex __cmf  ce_log2(  eComplex __z );
  183. eComplex __cmf  ce_log10( eComplex __z );
  184. eComplex __cmf  ce_pow( eComplex __base, eComplex __exponent );
  185. eComplex __cmf  ce_powReBase( extended __base, eComplex __exponent ); /* power of real base */
  186. eComplex __cmf  ce_powReExpo( eComplex __base, extended __exponent ); /* raise z to real power */
  187.                          /* for integer exponents, use ce_ipow ! */
  188. eComplex __cmf  ce_quartic( eComplex __z );  /* raise to the fourth power */
  189. eComplex __cmf  ce_sin(  eComplex __z );
  190. eComplex __cmf  ce_sinh( eComplex __z );
  191. eComplex __cmf  ce_square( eComplex __z );
  192. eComplex __cmf  ce_sqrt( eComplex __z );
  193. eComplex __cmf  ce_tan(  eComplex __z );
  194. eComplex __cmf  ce_tanh( eComplex __z );
  195.  
  196. #if defined __cplusplus && !defined _CMATH_CLASSDEFS
  197. }    //  end of the extern "C" statement
  198. #endif
  199.  
  200. #if defined __cplusplus && !defined __STD_COMPLEX && !defined __NEWCPLX_H
  201.    /* in addition to the basic operations defined above for C,
  202.       here is the same complete set of overloaded operators and
  203.       functions as offered by <newcplx.h> for the complex classes.  */
  204.  
  205.     inline extended real( eComplex _VFARC & __z )
  206.     {
  207.         return __z.Re;
  208.     }
  209.  
  210.     inline extended imag( eComplex _VFARC & __z )
  211.     {
  212.         return __z.Im;
  213.     }
  214.  
  215.     inline eComplex neg( eComplex _VFARC & __z1 )
  216.     {   eComplex Result;
  217.         Result.Re = -__z1.Re;
  218.         Result.Im = -__z1.Im;
  219.         return Result;
  220.     }
  221.  
  222.     inline eComplex conj( eComplex _VFARC & __z)
  223.     {   eComplex Result;
  224.         Result.Re =  __z.Re;
  225.         Result.Im = -__z.Im;
  226.         return Result;
  227.     }
  228.  
  229.     extended __cmf  norm( eComplex __z );
  230.     extended __cmf  arg(  eComplex __z );
  231.     eComplex __cmf  polar( extended Mag, extended Angle );
  232.       //  unary operators:
  233.  
  234.     inline